2 * Adium is the legal property of its developers, whose names are listed in the copyright file included
3 * with this source distribution.
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 * Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 #import <Adium/AIContactControllerProtocol.h>
18 #import <Adium/AIInterfaceControllerProtocol.h>
19 #import <Adium/AIMenuControllerProtocol.h>
20 #import "DCInviteToChatPlugin.h"
21 #import "DCInviteToChatWindowController.h"
22 #import <AIUtilities/AIMenuAdditions.h>
23 #import <Adium/AIAccount.h>
24 #import <Adium/AIChat.h>
25 #import <Adium/AIListContact.h>
26 #import <Adium/AIListGroup.h>
27 #import <Adium/AIListObject.h>
28 #import <Adium/AIMetaContact.h>
29 #import <Adium/AIService.h>
31 #define INVITE_CONTACT AILocalizedString(@"Invite to Chat",nil)
33 @interface DCInviteToChatPlugin (PRIVATE)
34 - (NSMenu *)groupChatMenuForContact:(AIListContact *)contact;
37 @implementation DCInviteToChatPlugin
42 shouldRebuildChatList = YES;
44 //Invite to Chat menu item
45 menuItem_inviteToChat = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:INVITE_CONTACT
47 action:@selector(dummyTarget:)
48 keyEquivalent:@""] autorelease];
49 [[adium menuController] addMenuItem:menuItem_inviteToChat toLocation:LOC_Contact_Action];
51 //Invite to Chat context menu item
52 menuItem_inviteToChatContext = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:INVITE_CONTACT
54 action:@selector(dummyTarget:)
55 keyEquivalent:@""] autorelease];
56 [[adium menuController] addContextualMenuItem:menuItem_inviteToChatContext toLocation:Context_Contact_Action];
60 //Validate our menu items
61 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
63 if (menuItem == menuItem_inviteToChat) {
65 AIListObject *object = [[adium interfaceController] selectedListObjectInContactList];
67 if ([object isKindOfClass:[AIListContact class]]) {
68 if ( shouldRebuildChatList ) {
69 [menuItem_inviteToChat setSubmenu:[self groupChatMenuForContact:(AIListContact *)object]];
72 return ([[menuItem_inviteToChat submenu] numberOfItems] > 0);
74 [menuItem_inviteToChat setTitle:INVITE_CONTACT]; // hit the reset button
78 } else if ( menuItem == menuItem_inviteToChatContext ) {
80 if ( shouldRebuildChatList ) {
81 AIListObject *object = [[adium menuController] currentContextMenuObject];
82 if ([object isKindOfClass:[AIListContact class]]) {
83 [menuItem_inviteToChatContext setSubmenu:[self groupChatMenuForContact:(AIListContact *)object]];
86 return ([[menuItem_inviteToChatContext submenu] numberOfItems] > 0);
93 - (IBAction)inviteToChat:(id)sender
95 NSArray *repArray = [sender representedObject];
96 AIListContact *listContact = [repArray objectAtIndex:1];
97 AIChat *chat = [repArray objectAtIndex:0];
99 [DCInviteToChatWindowController inviteToChatWindowForChat:chat contact:listContact];
104 - (NSMenu *)groupChatMenuForContact:(AIListContact *)contact
106 NSArray *openChats = [[adium interfaceController] openChats];
108 NSMenu *menu_chatMenu = nil;
109 NSDictionary *serviceDict;
110 NSString *serviceClass;
112 if (contact && ![contact isKindOfClass:[AIListGroup class]]) {
113 NSEnumerator *enumerator;
114 unsigned currentNumberOfItems, numberOfMenuItems = 0;
116 // Get a dictionary of (service class, contacts in that service)
117 serviceDict = ([contact isKindOfClass:[AIMetaContact class]] ?
118 [(AIMetaContact *)contact dictionaryOfServiceClassesAndListContacts] :
119 [NSDictionary dictionaryWithObject:contact forKey:[[contact service] serviceClass]]);
121 [menu_chatMenu setMenuChangedMessagesEnabled:NO];
123 enumerator = [serviceDict keyEnumerator];
124 while ((serviceClass = [enumerator nextObject])) {
126 //Each iteration, if we have more menu items now than before, add a separator item
127 currentNumberOfItems = [menu_chatMenu numberOfItems];
128 if (currentNumberOfItems > numberOfMenuItems) {
129 [menu_chatMenu addItem:[NSMenuItem separatorItem]];
130 numberOfMenuItems = currentNumberOfItems + 1;
133 // Loop through all chats
134 for (int i = 0; i < [openChats count]; i++) {
135 chat = [openChats objectAtIndex:i];
137 // Is this the same serviceClass as this contact?
138 if ( [[[[chat account] service] serviceClass] isEqualToString:serviceClass] ) {
140 // Is this a group chat?
141 if ([chat isGroupChat]) {
142 if (!menu_chatMenu) {
143 menu_chatMenu = [[[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:@""] autorelease];
146 if ( [menu_chatMenu indexOfItemWithTitle:[chat name]] == -1 ) {
147 NSMenuItem *menuItem;
148 menuItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:[chat name]
150 action:@selector(inviteToChat:)
152 [menuItem setRepresentedObject:[NSArray arrayWithObjects:chat,contact,nil]];
153 [menu_chatMenu addItem:menuItem];
161 //Remove the last separator if our new number of items isn't bigger than the previous one (that is, we haven't added any items since the last separator)
162 currentNumberOfItems = [menu_chatMenu numberOfItems];
163 if ((currentNumberOfItems <= numberOfMenuItems) &&
164 (currentNumberOfItems > 0)) {
166 [menu_chatMenu removeItemAtIndex:(currentNumberOfItems-1)];
169 [menu_chatMenu setMenuChangedMessagesEnabled:YES];
172 return menu_chatMenu;
175 // Dummy target so that we get validateMenuItem calls
176 - (IBAction)dummyTarget:(id)sender { }